import os
import logging, logging.handlers

BASE_DIR = os.path.dirname(__file__)


# 初始化日志配置
def init_log():
    # 1、初始化日志对象
    logger = logging.getLogger()
    # 2、设置日志级别
    logger.setLevel(logging.DEBUG)
    # 3、创建控制台日志处理器和文件日志处理器
    fh = logging.handlers.TimedRotatingFileHandler(os.path.join(BASE_DIR, 'log/log.log')
                                                   , when='midnight', interval=1, backupCount=5)
    sh = logging.StreamHandler()
    # 4、设置日志格式，创建格式化器
    fmt = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'
    formatter = logging.Formatter(fmt=fmt)
    # 5、将格式化器设置到日志器中
    fh.setFormatter(formatter)
    fh.setLevel(logging.INFO)
    sh.setFormatter(formatter)
    sh.setLevel(logging.INFO)
    # 6、将日志处理器添加到日志对象
    logger.addHandler(fh)
    logger.addHandler(sh)
